package com.hqyj.javaSpringBoot.modules.common.service.impl;

import com.hqyj.javaSpringBoot.modules.common.dao.ExceptionLogDao;
import com.hqyj.javaSpringBoot.modules.common.entity.ExceptionLog;
import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.service.ExceptionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.net.dns.ResolverConfiguration;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;


@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {
    @Autowired
    private ExceptionLogDao exceptionLogDao;

    @Override
    @Transactional
    public Result<ExceptionLog> insertExceptionLog(ExceptionLog exceptionLog) {
        exceptionLog.setCreatDate(LocalDateTime.now());
        List<ExceptionLog> exceptionLogs= Optional.ofNullable(
                exceptionLogDao.getExceptionLogByParam(
                        exceptionLog.getPath(),
                exceptionLog.getMethodName(),
                exceptionLog.getExceptionType()))
                .orElse(Collections.emptyList());
        if (exceptionLogs.size()==0){
            exceptionLogDao.insertExceptionLog(exceptionLog);
        }
        return new Result<ExceptionLog>(Result.ResultCode.success.code,"Insert success.",exceptionLog);
    }
}
